package com.ww.springboot.boot.algorithm.list.ergodic;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2021-12-25 16:10
 */
public class ThirdMax {
    public static void main(String[] args) {
        int[] nums = {1,-2147483648,2};
        System.out.println(method(nums));
    }

    private static int method(int[] nums) {
        long first = Long.MIN_VALUE;
        long second = Long.MIN_VALUE;
        long third = Long.MIN_VALUE;

        for (int i = 0; i < nums.length; i++) {
            long n = nums[i];
            if (n == first || n == second) {
                continue;
            }
            if (n >= first) {
                third = second;
                second = first;
                first = n;
            } else if (n > second) {
                third = second;
                second = n;
            } else if (n >= third) {
                third = n;
            }
        }
        return third == Long.MIN_VALUE ? (int) first : (int) third;
    }

}
